package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.table.CraftsmanFileTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**发型师附件表
 * @author : gaozifeng
 * @date : 2019/3/19 17:50
 */
@Component
public class CraftsmanFileJTO extends BaseJson {

	public static List<CraftsmanFileTO> createTableForModify(String json, long cid) throws Throwable {
		JsonNode root = validJson(json);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");

		if (!root.has("photo"))
			throw new InputParamException(InputParamException.Type.NULL, "图片");
		if (!root.path("photo").isArray())
			throw new InputParamException(InputParamException.Type.INVALID, "图片");
		List<CraftsmanFileTO> list = new ArrayList<>(5);
		for (Iterator<JsonNode> iterator = root.path("photo").elements(); iterator.hasNext();) {
			JsonNode node = iterator.next();
			list.add(createTableForNew(node, cid));
		}
		return list;
	}

	public static CraftsmanFileTO createTableForNew(JsonNode root, long jobseekerId) throws Throwable {
		return new CraftsmanFileTO().setType(validString(root,"type",2, YesNoEnum.YES)).setPath(validString(root,"path",300,YesNoEnum.YES)).setJobseeker_id(jobseekerId).setIsDelete(YesNoEnum.NO);
	}

	public static ObjectNode toJson(CraftsmanFileTO table) throws Throwable {
		ObjectNode root = MAPPER.createObjectNode();

		if (table != null) {
			root.put("path",table.getPath());
			root.put("type",table.getType());
			root.put("jobseekerId",table.getJobseeker_id());
			root.put("jobseekerCode",table.getCode());
			System.out.println(table.getCraftsmanName());
			root.put("craftsName",table.getCraftsmanName());
			toBaseJson(root, table);
		}

		return root;
	}

	public static ArrayNode toJson(List<CraftsmanFileTO> tables) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if (!CommonUtil.isEmpty(tables))
			for (Iterator<CraftsmanFileTO> iterator = tables.iterator(); iterator.hasNext();) {
				root.add(toJson(iterator.next()));
			}

		return root;
	}
}
